'use strict';

const BaseModel = require('./BaseModel');

/**
 * 地区 表信息
 */
class SysMacAddrModel extends BaseModel {
  /*status: 1.正常使用，3.禁用，2.已删除
   */
  constructor() {
    super('sys_macaddr', 'id');

    this.entity = class entity {
      constructor() {
        this.id = null;
        this.macAddr = null;
        this.creator = null;
        this.createTime = null;
        this.updateTime = null;
        this.status = null;
        this.remarks = null;
        this.userId = null;
      }
    };
  }

  /**
   * 添加macAddr
   */
  async addMacAddr(app, models) {
    return await app.mysql.insert('sys_macaddr', models);
  }

  /**
   * 删除macAddr
   */
  async delMacAddr(app, id) {
    return app.mysql.delete('sys_macaddr', {
      id
    })
  }

  /**
   * 修改macAddr
   */
  async updateMacAddr(app, id, models) {
    return app.mysql.update('sys_macaddr', models, {
      where: {
        id,
      },
    });
  }

  /**
   * macAddr列表
   */
  async findMacAddrList(app, options) {
    const pageSize = parseInt(options.pageSize || 10);
    let pageIndex = parseInt(options.pageIndex || 0);
    if (pageIndex - 1 <= 0) {
      pageIndex = 1;
    }
    let sql = ' select * from sys_macaddr  where 1=1 and status !=2 ';
    if (options.macAddr) {
      sql += `and mac_addr like \'%${options.macAddr}%\' `;
    }
    if (options.remarks) {
      sql += `and remarks like \'%${options.remarks}%\' `;
    }
    if (options.startDate && options.endDate) {
      sql += ` and create_time >= '${options.startDate}' and create_time < '${options.endDate}' `;
    }
    const countSql = `select count(1) as count from (${sql}) a`;
    const totalCount = await app.mysql.query(countSql);
    const totalPage = Math.ceil(totalCount[0].count / pageSize);
    sql += ` order by create_time desc limit ${(pageIndex - 1) * pageSize},${pageSize}`;
    const results = await app.mysql.query(sql);
    const macAddrList = [];
    if (results.length > 0) {
      for (let dbentity of results) {
        let macAddr = this.transformation_Entity(dbentity);
        macAddrList.push(macAddr);
      }
    }
    return {
      pageIndex,
      pageSize,
      totalPage,
      totalCount: totalCount[0] && totalCount[0].count || 0,
      macAddrList,
    };
  }

  async findMacAddrCode(app, macAddr) {
    const sql = `select * from sys_macaddr where mac_addr = '${macAddr}' `;
    return await app.mysql.query(sql);
  }

  async findNormalMacAddr() {
    const sql = ' select * from sys_macaddr  where 1=1 and status !=2 ';
  }

  async findUserById(app, userId) {
    const sql = `select id from admin_users where id = '${userId}'`;
    return await app.mysql.query(sql);
  }

  async findAllMacAddr(app) {
    const sql = `select mac_addr from sys_macaddr  where 1=1 and status =1 `;
    return await app.mysql.query(sql);
  }

}

module.exports = SysMacAddrModel;